#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        unordered_map<int, int> hash;
        for (int i = 0; i < arr.size(); i++) hash[arr[i]] = i;
        int n = arr.size(), ret = 0;
        vector<vector<int>> dp(n, vector<int>(n, 2));
        for (int i = 2; i < n; i++)
        {
            for (int j = 1; j < i; j++)
            {
                if (arr[i] - arr[j] < arr[j] && hash.count(arr[i] - arr[j]))
                    dp[i][j] = dp[j][hash[arr[i] - arr[j]]] + 1;
                ret = max(ret, dp[i][j]);
            }
        }
        return ret == 2 ? 0 : ret;
    }
};